package com.autonavi.yunda.yunji.controller.request;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.common.utils.JsonUtils;
import com.autonavi.yunda.yunji.core.engine.argresolver.ParamResolver;
import com.autonavi.yunda.yunji.core.enums.ParamSourceType;
import com.autonavi.yunda.yunji.core.vo.HttpRequest;
import com.autonavi.yunda.yunji.core.vo.param.Param;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cizhong.wcs
 * @date 2022/3/3 4:35 下午
 */
@Data
public class EngineDebugAllRequest {
    @NotNull
    private Long id;
    @Valid
    private List<Param> inputParams;
    /**
     * 导出文件类型-目前支持csv、xls
     */
    private String exportType;
    /**
     * 编排类型 task:任务 interface:接口
     */
    private String editType;

    public HttpRequest toHttpRequest(MultiValueMap<String, MultipartFile> multiValueMap) {
        HttpRequest httpRequest = new HttpRequest();
        Map<String, String[]> paramMap = inputParams.stream()
                .filter(p -> ParamSourceType.query.equals(p.getSource()))
                .filter(p -> {
                    if("string".equals(p.type)) {
                        p.resetDebugValueBasedOnFlag();
                        return true;
                    }else {
                        return Strings.isNotBlank(p.debugValue);
                    }})
                .collect(Collectors.toMap(Param::getName, p -> new String[]{p.debugValue}));
        Map<String, String> headersMap = inputParams.stream()
                .filter(p -> ParamSourceType.headers.equals(p.getSource()))
                .filter(p -> {
                    if("string".equals(p.type)) {
                        p.resetDebugValueBasedOnFlag();
                        return true;
                    }else {
                        return Strings.isNotBlank(p.debugValue);
                    }})
                .collect(Collectors.toMap(p -> p.getName().toLowerCase(), Param::getDebugValue));
        AmapPreconditions.checkArgumentForUser(
                inputParams.stream().map(Param::getName).distinct().count() == inputParams.size(),
                "start节点入参列表不允许存在相同名称的参数"
        );
        Map<String, Object> bodyMap = new HashMap<>(8);
        inputParams.stream()
                .filter(p -> ParamSourceType.body.equals(p.getSource()))
                .filter(p -> {
                    if("string".equals(p.type)) {
                        p.resetDebugValueBasedOnFlag();
                        return true;
                    }else {
                        return Strings.isNotBlank(p.debugValue);
                    }})
                .forEach(p -> bodyMap.put(p.name, ParamResolver.getParamValueByType(p.getDebugValue(), p.type)));
        String body = bodyMap.isEmpty() ? null : JsonUtils.toString(bodyMap);
        httpRequest.setParameterMap(paramMap);
        httpRequest.setBody(body);
        httpRequest.setMultiFileMap(multiValueMap);
        httpRequest.setHeaders(headersMap);
        return httpRequest;
    }
}
